package nl.project.data;

import java.util.ArrayList;

import nl.project.data.controller.MembersDAO;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class Parent extends User{
	private int parentId;
	private ArrayList<Object> students;
	private Key key;
	private String[] address;
private String phoneNumber;
private String lattitude;
private String longitude;

	public Parent(int id) throws EntityNotFoundException {
		address = new String[5];
		this.parentId = id;
		
		//get all info from this parent
		getParentInfo();
	}

	/**
	 * Get all the info from a certain parent
	 * 
	 * @throws EntityNotFoundException
	 */

	private void getParentInfo() throws EntityNotFoundException {
		Key k = KeyFactory.createKey("Parents", "Parents");
		Entity e = MembersDAO.getEntityInfo(this.parentId, k, "parentId", "Parent");
		this.initials = (String) e.getProperty("initials");
		this.firstName = (String) e.getProperty("firstName");
		this.surName = (String) e.getProperty("surName");
		this.title = (String) e.getProperty("title");
		this.preposition = (String) e.getProperty("preposition");
		this.key = e.getKey();
		this.address[0] = (String) e.getProperty("addressLine");
		this.address[1] = (String) e.getProperty("addressNumber");
		this.address[2] = (String) e.getProperty("zipCode");
		this.address[3] = (String) e.getProperty("residence");
		this.address[4] = (String) e.getProperty("emailAddress");
		this.phoneNumber = (String) e.getProperty("cellPhoneNumber");
		this.lattitude = (String) e.getProperty("lat");
		this.longitude = (String) e.getProperty("long");
		getStudents(e);
	}
	
	@SuppressWarnings("unchecked")
	private void getStudents(Entity e) {
		this.students = (ArrayList<Object>) e.getProperty("studentIDs");
	}
	
	/**
	 * Return all the studentIDs (children) of the found parent.
	 */
	public ArrayList<Object> getStudents() {
		return this.students;
	}

	/**
	 * @return the parentId
	 */
	public int getParentId() {
		return parentId;
	}

	/**
	 * @param parentId the parentId to set
	 */
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	/**
	 * @param students the students to set
	 */
	public void setStudents(ArrayList<Object> students) {
		this.students = students;
	}
	
	public String getLattitude() {
		return lattitude;
	}

	public String getLongitude() {
		return longitude;
	}
	
	public Key getKey(){
		return this.key;
	}

	public String getAddress() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.address[0] + " ");
		sb.append(this.address[1] + " ");
		sb.append(this.address[2] + " ");
		sb.append(this.address[3]);
		return sb.toString();
	}
	
	public String[] getAddressArray() {
		return address;
	}
	
	public String getPhoneNumber() {
		return phoneNumber;
	}
}